
import dotenv
from langchain_openai import ChatOpenAI
from langgraph.constants import START,END
from langgraph.graph import add_messages, StateGraph

from typing import TypedDict, Annotated

dotenv.load_dotenv()

class ChatBotState(TypedDict):
    """聊天消息就是状态，消息是一个字典，add_messages就是一个归纳函数"""
    messages:Annotated[list,add_messages]
    node_name:str

llm =ChatOpenAI(model="gpt-4o-mini")

def chat_bot(state:ChatBotState)->ChatBotState:
    """聊天机器人节点函数"""
    ai_message = llm.invoke(state["messages"])

    return {"messages":[ai_message],"node_name":"chat_bot"}

# 构建图架构
graph_builder = StateGraph(ChatBotState)

# 添加聊天节点
graph_builder.add_node("llm",chat_bot)
# 通过边连接点
graph_builder.add_edge(START,"llm")
graph_builder.add_edge("llm",END)
# 编译图架构
graph_bin = graph_builder.compile()

# 运行图架构
print(graph_bin.invoke({"messages":[("human","你好你是？")],"node_name":"star_bot"}))